package org.loofer.uikit;

import android.database.DataSetObservable;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import java.util.ArrayList;
import java.util.Iterator;

public class MergeAdapter implements ListAdapter {
    private final ArrayList<ListAdapter> mAdapters = new ArrayList();
    private final DataSetObservable mDataSetObservable = new DataSetObservable();

    public void addAdapter(ListAdapter adapter) {
        if (!this.mAdapters.contains(adapter)) {
            this.mAdapters.add(adapter);
        }
    }

    public void removeAdapter(ListAdapter adapter) {
        if (this.mAdapters.contains(adapter)) {
            this.mAdapters.remove(adapter);
        }
    }

    public int getAdapterCount() {
        return this.mAdapters.size();
    }

    public ListAdapter getAdapterAt(int index) {
        return (ListAdapter) this.mAdapters.get(index);
    }

    public boolean areAllItemsEnabled() {
        Iterator it = this.mAdapters.iterator();
        while (it.hasNext()) {
            if (!((ListAdapter) it.next()).areAllItemsEnabled()) {
                return false;
            }
        }
        return true;
    }

    public boolean isEnabled(int position) {
        if (position < 0) {
            throw new ArrayIndexOutOfBoundsException("invalid position " + position);
        }
        int pos = position;
        Iterator it = this.mAdapters.iterator();
        while (it.hasNext()) {
            ListAdapter adapter = (ListAdapter) it.next();
            int count = adapter.getCount();
            if (pos < count) {
                return adapter.isEnabled(pos);
            }
            pos -= count;
        }
        throw new ArrayIndexOutOfBoundsException("invalid position " + position);
    }

    public void registerDataSetObserver(DataSetObserver observer) {
        this.mDataSetObservable.registerObserver(observer);
        Iterator it = this.mAdapters.iterator();
        while (it.hasNext()) {
            ((ListAdapter) it.next()).registerDataSetObserver(observer);
        }
    }

    public void unregisterDataSetObserver(DataSetObserver observer) {
        this.mDataSetObservable.unregisterObserver(observer);
        Iterator it = this.mAdapters.iterator();
        while (it.hasNext()) {
            ((ListAdapter) it.next()).unregisterDataSetObserver(observer);
        }
    }

    public void notifyDataSetChanged() {
        this.mDataSetObservable.notifyChanged();
    }

    public void notifyDataSetInvalidated() {
        this.mDataSetObservable.notifyInvalidated();
    }

    public int getCount() {
        int count = 0;
        Iterator it = this.mAdapters.iterator();
        while (it.hasNext()) {
            count += ((ListAdapter) it.next()).getCount();
        }
        return count;
    }

    public Object getItem(int position) {
        if (position < 0) {
            throw new ArrayIndexOutOfBoundsException("invalid position " + position);
        }
        int pos = position;
        Iterator it = this.mAdapters.iterator();
        while (it.hasNext()) {
            ListAdapter adapter = (ListAdapter) it.next();
            int count = adapter.getCount();
            if (pos < count) {
                return adapter.getItem(pos);
            }
            pos -= count;
        }
        throw new ArrayIndexOutOfBoundsException("invalid position " + position);
    }

    public long getItemId(int position) {
        return (long) position;
    }

    public boolean hasStableIds() {
        if (this.mAdapters.isEmpty()) {
            return false;
        }
        Iterator it = this.mAdapters.iterator();
        while (it.hasNext()) {
            if (!((ListAdapter) it.next()).hasStableIds()) {
                return false;
            }
        }
        return true;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (position < 0) {
            throw new ArrayIndexOutOfBoundsException("invalid position " + position);
        }
        int pos = position;
        Iterator it = this.mAdapters.iterator();
        while (it.hasNext()) {
            ListAdapter adapter = (ListAdapter) it.next();
            int count = adapter.getCount();
            if (pos < count) {
                return adapter.getView(pos, convertView, parent);
            }
            pos -= count;
        }
        throw new ArrayIndexOutOfBoundsException("invalid position " + position);
    }

    public int getItemViewType(int position) {
        if (position < 0) {
            throw new ArrayIndexOutOfBoundsException("invalid position " + position);
        }
        int pos = position;
        int offset = 0;
        Iterator it = this.mAdapters.iterator();
        while (it.hasNext()) {
            ListAdapter adapter = (ListAdapter) it.next();
            int count = adapter.getCount();
            if (pos < count) {
                return adapter.getItemViewType(pos) + offset;
            }
            pos -= count;
            offset += adapter.getViewTypeCount();
        }
        throw new ArrayIndexOutOfBoundsException("invalid position " + position);
    }

    public int getViewTypeCount() {
        int count = 0;
        Iterator it = this.mAdapters.iterator();
        while (it.hasNext()) {
            count += ((ListAdapter) it.next()).getViewTypeCount();
        }
        return max(count, 1);
    }

    public boolean isEmpty() {
        Iterator it = this.mAdapters.iterator();
        while (it.hasNext()) {
            if (!((ListAdapter) it.next()).isEmpty()) {
                return false;
            }
        }
        return true;
    }

    private static int max(int value1, int value2) {
        return value1 > value2 ? value1 : value2;
    }
}
